<template>
	<div>
		<div style="font-weight: bold; font-size: 15px;margin-bottom: 20px;">修改用户状态</div>
		<el-form ref="userStateForm" :model="userStateForm" label-width="80px">
			<!-- <el-form-item label="昵称" prop="nickname">
				<el-input v-model="userStateForm.nickname" placeholder="请输入昵称" />
			</el-form-item> -->
			<el-form-item label="用户类型" prop="status">
				<el-select v-model="userStateForm.usertype" placeholder="修改用户类型" clearable style="width: 240px">
					<el-option v-for="dict in dict.type.bbs_user_type" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="用户状态" prop="status">
				<el-select v-model="userStateForm.userstate" placeholder="修改用户状态" clearable style="width: 240px">
					<el-option v-for="dict in dict.type.bbs_user_state" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item
				v-if="userStateForm.usertype!=userOldForm.usertype && userStateForm.usertype==2"
				label="禁言时长(天)" prop="bannedTime">
				<el-input v-model="operatinUserForm.bannedTime" placeholder="请输入禁言时间" type="number" />
			</el-form-item>
			<el-form-item
				v-if="userStateForm.usertype!=userOldForm.usertype || userStateForm.userstate!=userOldForm.userstate"
				label="操作记录备注" prop="note">
				<el-input v-model="operatinUserForm.note" placeholder="请输入操作记录备注" />
			</el-form-item>
			
		</el-form>
		<div style="font-weight: bold; font-size: 15px;margin-bottom: 20px;">用户最近禁言记录</div>
		<el-table :data="bannedRecordList" >
			<el-table-column label="备注" align="center" prop="note" :show-overflow-tooltip="true" />
			<el-table-column label="禁言开始时间" align="center" prop="operateTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.operateTime)}}</span>
				</template>
			</el-table-column>
			<el-table-column label="禁言结束时间" align="center" prop="bannedTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.bannedTime) }}</span>
				</template>
			</el-table-column>

		</el-table>
	</div>
</template>

<script>
	import {
		listBbs_user,
		updateBbs_user,
		getUserSchool
	} from "@/api/bbs_user/bbs_user";
	import {
		listBbs_operation_record,
		addBbs_operation_record,
		list_banned_record
	} from "@/api/bbs_operation_record/bbs_operation_record";
	export default {
		name: 'userUpdateFrom',
		props: {
			currentUserId: String,
			currentSchoolId:Number,
		},
		dicts: ['bbs_true_or_false','bbs_user_type','bbs_user_state'],
		data() {
			return {
				userStateForm:{},
				rules:'',
				userOldForm:{},
				operatinUserForm: {
					schoolId: null,
					recordState: 1,
					operator: null,
					targetKind: null,
					targetId: null,
					changeInfo: null,
					note: null,
					bannedTime: null,
				},
				updateUserObj:{},
				// 查询参数
				bannedQueryParams: {
					pageNum: 1,
					pageSize: 5,
					schoolId: null,
					recordState: null,
					operator: null,
					targetKind: null,
					targetId: null,
					changeInfo: null,
					note: null,
					operateTime: null,
					bannedTime: null,
				},
				bannedRecordList:[]
			}
		},
		created() {
			console.log("组建中",this.currentUserId,this.currentSchoolId)
			this.getUserInfo()
			this.getUserBannedList()
		},
		// activated() {
		// 	this.getUserInfo(this.row)
		// },
		methods: {
			//获取用户信息
			getUserInfo(){
				//获取发帖用户信息
				let userSchoolId =this.$store.state.user.currentSchool.schoolid
				if (this.currentSchoolId) {
					userSchoolId= this.currentSchoolId;
				}
				let getParams ={
					// schoolId: this.$store.state.user.currentSchool.schoolid,
					schoolId: userSchoolId,
					userId: this.currentUserId,
				}
				getUserSchool(getParams).then(response => {
					console.log(response)
					this.userStateForm = response.data;
					//字典需要处理为字符型
					this.userStateForm.userstate = this.userStateForm.userstate.toString()
					this.userStateForm.usertype = this.userStateForm.usertype.toString()
					//将此时的form复制一份，存起来
					this.userOldForm = JSON.parse(JSON.stringify(this.userStateForm));
					this.operatinUserForm.note = null;
				});
			},
			async getUserBannedList() {
				this.bannedQueryParams.schoolId = this.$store.state.user.currentSchool.schoolid;
				if(this.currentSchoolId){
					this.bannedQueryParams.schoolId =  this.currentSchoolId
				}
				this.bannedQueryParams.targetKind = 1;
				this.bannedQueryParams.targetId = this.currentUserId;
				this.bannedQueryParams.bannedTime = new Date().toISOString();
				const response = await list_banned_record(this.bannedQueryParams);
				if (response.code == 200) {
					console.log(response)
					for (var i = 0; i < response.rows.length; i++) {
						response.rows[i].changeInfo = JSON.parse(response.rows[i].changeInfo)
						response.rows[i].operatorName = ""
						// if (response.rows[i].operator.length > 30) {
						// 	const userResponse = await getBbs_user(response.rows[i].operator);
						// 	console.log('用户信息', userResponse, i);
						// 	response.rows[i].operatorName = userResponse.data.nickname;
						// }
					}
					this.bannedRecordList = response.rows;
					console.log(this.bannedRecordList)
				}
			},
			//修改用户状态
			updateUserInfo(){
				//如果是从其他状态改为禁言状态，且没有禁言时间的话，
				if(this.userStateForm.usertype==2 && this.userStateForm.usertype != this.userOldForm.usertype && !this.operatinUserForm.bannedTime){
					// this.$message.error('请输入禁言时间');
					// this.loading = false;
					// return 0;
					// this.operatinForm.bannedTime = null
					this.operatinUserForm.note=this.operatinUserForm.note+" 永久禁言"
				}
				this.updateUserObj.userid = this.userStateForm.userid;
				this.updateUserObj.userstate = this.userStateForm.userstate;
				this.updateUserObj.usertype = this.userStateForm.usertype;
				this.updateUserObj.schoolid = this.$store.state.user.currentSchool.schoolid;
				if(this.currentSchoolId){
					this.updateUserObj.schoolid = this.currentSchoolId
				}
				// console.log("updateUserObj", this.updateUserObj);
				updateBbs_user(this.updateUserObj).then((response) => {
					console.log('用户', response)
					if (response.code == 200) {
						this.$modal.msgSuccess("用户状态修改成功");
						this.createUserOperationRecord(1, this.userOldForm, this.userStateForm)
						this.updateUserObj = {
							userid: "",
							userType: "",
							userState: ""
						}	
					} else {
						this.$modal.msgError(response.msg);
					}
				});
			},
			//创建对用户的操作记录
			createUserOperationRecord(targetKind,oldInfo,newInfo) {
				console.log("触发")
				//构建操作记录
				this.operatinUserForm.schoolId = this.$store.state.user.currentSchool.schoolid;
				if(this.currentSchoolId){
					this.operatinUserForm.schoolId = this.currentSchoolId
				}
				this.operatinUserForm.operator = this.$store.state.user.name;
				this.operatinUserForm.targetKind = targetKind;
				//操作对象为用户
				if (targetKind==1) {
					this.operatinUserForm.targetId = newInfo.userid;
				}
				this.operatinUserForm.changeInfo = {
					data: {
						oldForm: {},
						newForm: {}
					}
				};
				//获取变更了属性值
				let haveChange =0;
				for (let key in newInfo) {
					let value = newInfo[key];
					// 在这里对属性进行处理
					// console.log(key + ': ' + typeof newInfo[key]);
					if (typeof newInfo[key] !='object' &&newInfo[key]!=oldInfo[key]) {
						haveChange=1
						this.operatinUserForm.changeInfo.data.oldForm[key] =oldInfo[key]
						this.operatinUserForm.changeInfo.data.newForm[key] =newInfo[key]
					}
				}
				if (haveChange==1) {
					//如果存在禁言时间
					if (this.operatinUserForm.bannedTime) {
						this.operatinUserForm.note=this.operatinUserForm.note+" 禁言"+ this.operatinUserForm.bannedTime*1 +"天"
						const bannedtime = new Date(new Date().getTime() + this.operatinUserForm.bannedTime*24 * 60 * 60 * 1000)
						this.operatinUserForm.bannedTime = this.formatDateTime(bannedtime)
						console.log(this.operatinUserForm.bannedTime)
					}
					this.operatinUserForm.changeInfo = JSON.stringify(this.operatinUserForm.changeInfo)
					console.log(this.operatinUserForm)
					addBbs_operation_record(this.operatinUserForm).then(response => {
						this.$modal.msgSuccess("操作记录新增成功");
					});
				}
			},
			formatDateTime(date) {
				//date是传入的时间
				let d = new Date(date);
				let month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
				let day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
				let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
				let min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
				let sec = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
				let times = d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ":" + sec;
				return times;
			},
		},
		watch: {
			"currentUserId": {
				handler(newVal, oldVal) {
					console.log("watch触发")
					this.getUserInfo()
					this.getUserBannedList()
				},
				deep: true
			}
		}
	}
</script>

<style>
	.FormItem {
		padding: 10px;
		border: 1px solid #000;
		margin-bottom: 10px;
		border-radius: 10px;
	}

	.FormList {
		padding: 0 10px;
	}

	/* 横线 */
	.line {
		float: right;
		width: 100%;
		height: 1px;
		margin-top: -0.5em;
		background: #000000;
		position: relative;
		text-align: center;
		margin: 10px 0;
	}
</style>